//
//  YXTabBarController.m
//  guide
//
//  Created by 周丽丽 on 16/8/22.
//  Copyright © 2016年 周丽丽. All rights reserved.
//

#import "YXTabBarController.h"
#import "YXTaskListTableViewController.h"
#import "YXSettingViewController.h"
@interface YXTabBarController ()<UITabBarControllerDelegate>
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) NSArray *tabButtons;
@property (nonatomic, strong) UIButton *tasListkButton;
@property (nonatomic, strong) UIButton *settingButton;
@property (nonatomic, strong) UIView *VerticalLineView;
@end
@implementation YXTabBarController
+ (void)initialize{
    UINavigationBar *navBar = [UINavigationBar appearance];
    [navBar setBarTintColor:[UIColor colorWithHexString:@"ff7000"]];
    [navBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],
                                     NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupChildControllers];
    self.delegate = self;
    [self setupTabbarSubViews];
    [self layoutTabbarSubViews];
    [self setSelectedIndex:0];
}
- (void)setupChildControllers{
    UINavigationController *listVc = [[UINavigationController alloc]initWithRootViewController:[[YXTaskListTableViewController alloc]init]];
    UINavigationController *settingVC = [[UINavigationController alloc]initWithRootViewController:[[YXSettingViewController alloc]init]];
    self.viewControllers = @[listVc,settingVC];
}
- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    [super setSelectedIndex:selectedIndex];
    for (UIButton *button in self.tabButtons) {
        button.selected = NO;
    }
    UIButton *button = self.tabButtons[selectedIndex];
    button.selected = YES;
}
#pragma mark - UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    NSInteger index = [self.viewControllers indexOfObject:viewController];
    for (UIButton *button in self.tabButtons) {
        button.selected = NO;
    }
    UIButton *button = self.tabButtons[index];
    button.selected = YES;
}
#pragma mark - custom Tabbar
- (void)setupTabbarSubViews {
    UIView *bgView = [[UIView alloc]initWithFrame:self.tabBar.bounds];
    bgView.clipsToBounds = NO;
    bgView.userInteractionEnabled = NO;
    bgView.backgroundColor = [UIColor colorWithHexString:@"fcfcfc"];
    [self.tabBar addSubview:bgView];
    self.bgView = bgView;
    
    UIButton *tasListkButton = [self bottemButtonWithTitle:@"任务"];
    [self.bgView addSubview:tasListkButton];
    self.tasListkButton = tasListkButton;
    
    UIView *VerticalLineView = [[UIView alloc] init];
    VerticalLineView.backgroundColor = [UIColor colorWithHexString:@"e2e2e4"];
    [self.bgView addSubview:VerticalLineView];
    self.VerticalLineView = VerticalLineView;
    
    UIButton *settingButton = [self bottemButtonWithTitle:@"设置"];
    [self.bgView addSubview:settingButton];
    self.settingButton = settingButton;
    
    self.tabButtons = @[tasListkButton, settingButton];
}
- (void)layoutTabbarSubViews{
    [self.tasListkButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView.mas_centerX).offset(-self.bgView.width * 0.25);
        make.top.equalTo(self.bgView).offset(4);
        make.bottom.equalTo(self.bgView).offset(-6);
    }];
    [self.VerticalLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.bgView);
        make.centerY.equalTo(self.bgView);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(20 * AutoSizeScaleX);
    }];
    [self.settingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView.mas_centerX).offset(self.bgView.width * 0.25);
        make.top.equalTo(self.bgView).offset(4);
        make.bottom.equalTo(self.bgView).offset(-6);
    }];
}
- (UIButton *)bottemButtonWithTitle:(NSString *)title {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:11];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [btn setTitleColor:[UIColor colorWithHexString:@"5e5e5e"] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithHexString:@"ff4600"] forState:UIControlStateSelected];
    UIImage *iconNormal = [UIImage imageNamed:[title stringByAppendingString:@"icon正常状态"]];
    UIImage *iconSelected = [UIImage imageNamed:[title stringByAppendingString:@"icon当前选择状态"]];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setImage:iconNormal forState:UIControlStateNormal];
    [btn setImage:iconSelected forState:UIControlStateSelected];
    btn.imageEdgeInsets = UIEdgeInsetsMake(-12,-(btn.frame.size.width - iconNormal.size.width) * 0.5,0,0);
    btn.titleEdgeInsets = UIEdgeInsetsMake(30,btn.frame.size.width - iconNormal.size.width * 0.5 - 8, 0, 0);
    return btn;
}

@end
